package com.example.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.result.Result;
import com.example.common.result.YiFeiException;
import com.example.entity.hz.Messageconfiguration;
import com.example.entity.hz.Printtemplate;
import com.example.service.MessageconfigurationService;
import com.example.mapper.MessageconfigurationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 14501
* @description 针对表【messageConfiguration】的数据库操作Service实现
* @createDate 2024-09-06 09:03:07
*/
@Service
public class MessageconfigurationServiceImpl extends ServiceImpl<MessageconfigurationMapper, Messageconfiguration>
    implements MessageconfigurationService{

    @Resource
    private MessageconfigurationMapper messageconfigurationMapper;

    @Override
    @Transactional
    public Result saveMessageConfiguration(List<Messageconfiguration> messageconfigurationList) {
        try{
            if (!messageconfigurationList.isEmpty()){
                messageconfigurationList.forEach(arr -> {
                    arr.setCreateTime(DateUtil.format(new Date(), "yyyyMMdd"));
                    arr.setStatus("开启");
                    this.messageconfigurationMapper.insert(arr);
                });
            }
            return Result.success();
        }catch (Exception e){
            throw new YiFeiException(500, "消息模板新增发生异常"+e.getMessage());
        }
    }

    @Override
    public Result deleteMessageConfiguration(List<Integer> uidList) {
        try {
            if (!uidList.isEmpty()){
                boolean b = this.removeByIds(uidList);
                if (b){
                    return Result.success();
                }
            }
            return Result.error("请输入正确参数");
        }catch (Exception e){
            throw new YiFeiException(500,"消息通知模版删除发生异常"+e.getMessage());
        }
    }

    @Override
    public Result<Messageconfiguration> queryOneMessageConfiguration(Integer uid) {
        try {
            Messageconfiguration messageconfiguration = this.messageconfigurationMapper.selectById(uid);
            return Result.success(messageconfiguration);
        }catch (Exception e){
            throw new YiFeiException(500,"通过uid查询消息通知模版详细信息发生异常"+e.getMessage());
        }
    }

    @Override
    public Result updateMessageConfiguration(Messageconfiguration messageconfiguration) {
        try {
            messageconfiguration.setUpdateTime(DateUtil.format(new Date(), "yyyyMMdd"));
            int update = this.messageconfigurationMapper.updateById(messageconfiguration);
            if (update>0){
                return Result.success();
            }
            return Result.error("修改失败!请检查传参uid");
        }catch (Exception e){
            throw new YiFeiException(500, "消息通知模版修改发生异常"+e.getMessage());
        }
    }
}




